define(["moment", "module/modulesSPC/program/B0504/qcItemEditService"], function (moment) {
    return ["$scope", "$filter", "$stateParams", "config", "MMWService", "$calculater", "QcItemEditService", "$datePicker",
    function ($scope, $filter, $stateParams, config, $MMWService, $calculater, $QcItemEditService, $datePicker) {
        $scope.init = function(){
            $scope.workItem = {};
            $scope.workItem.plan_insp_date = moment().format('YYYY-MM-DD'); //預設今日
            $scope.workItem.is_urgent = 'N'; //預設非緊急批
            $scope.qc_item_detail = [];
        }

        $scope.back = function(){
            $scope.$root.hideMenu();
            $scope.$root.changeProgram('home', true);
        }

        //讀取品號類別
        $scope.loadItemType = function(){
            var typeList = [
                {code : '0', name: $filter('translate')('B0504.item_type_0')}, //品號類別
                {code : '1', name: $filter('translate')('B0504.item_type_1')}  //品號
            ];
            $scope.$root.showSelect({
                title : $filter('translate')('common.msg.plz_select'),
                label : 'name',
                code : 'code',
                order : 'code',
                selectCode : $scope.workItem.item_type || '',
                list : typeList,
                confirm : function(selectItem, TypeDialog){
                    if($scope.workItem.item_type != selectItem.code){
                        $scope.workItem.item_type = selectItem.code;
                        $scope.workItem.item_type_display = selectItem.name;
                        $scope.workItem.item_no = ''; //品號編號
                        $scope.workItem.item_name = ''; //品號名稱
                        $scope.workItem.item_spec = ''; //規格
                        $scope.workItem.qc_category = ''; //品管類別
                        $scope.workItem.article_no = ''; //貨號
                        $scope.workItem.unit = ''; //單位
                    }
                    TypeDialog.hide();
                }
            });
        }

        //讀取品號
        $scope.loadItemNo = function(){
            if($scope.workItem.item_type == null || $scope.workItem.item_type == ''){
                $scope.$root.showAlert($filter('translate')('B0504.msg.item_type_empty'));
            }else{
                $scope.$root.showQueryList({
                    key:'item_no',
                    pageRecord:50,
                    header : [
                        {label: $filter('translate')('B0504.queryList.item_no'), code:'item_no'},
                        {label: $filter('translate')('B0504.queryList.item_name'), code:'item_name'},
                        {label: $filter('translate')('B0504.queryList.item_spec'), code:'item_spec'},
                        {label: $filter('translate')('B0504.queryList.qc_category'), code:'qc_category'},
                        {label: $filter('translate')('B0504.queryList.article_no'), code:'article_no'},
                        {label: $filter('translate')('B0504.queryList.unit'), code:'unit'}
                    ],
                    query: function(conditions, feedback){
                        var content = {
                            qry_type: $scope.workItem.item_type //0：產品  1：物料
                        }
                        productInfoGet(content , function(data){
                            if(data.product_info_get){
                                feedback(data.product_info_get);
                            }
                        });
                    },
                    confirm: function(selectItem, dialog){
                        dialog.hide();
                        $scope.workItem.item_no = selectItem.item_no; //品號編號
                        $scope.workItem.item_name = selectItem.item_name; //品號名稱
                        $scope.workItem.item_spec = selectItem.item_spec; //規格
                        $scope.workItem.qc_category = selectItem.qc_category; //品管類別
                        $scope.workItem.article_no = selectItem.article_no; //貨號
                        $scope.workItem.unit = selectItem.unit; //單位
                        defaultQcItemGet_Imp();
                    }
                });
            }
        }

        //讀取檢驗類型 2：首檢 3：巡檢
        $scope.loadInspectType = function(){
            var typeList = [
                {code : '2', name: $filter('translate')('B0504.inspect_type_2')},
                {code : '3', name: $filter('translate')('B0504.inspect_type_3')}
            ];
            $scope.$root.showSelect({
                title : $filter('translate')('common.msg.plz_select'),
                label : 'name',
                code : 'code',
                order : 'code',
                selectCode : $scope.workItem.inspect_type || '',
                list : typeList,
                confirm : function(selectItem, TypeDialog){
                    $scope.workItem.inspect_type = selectItem.code;
                    $scope.workItem.inspect_type_display = selectItem.name;
                    TypeDialog.hide();
                    defaultQcItemGet_Imp();
                }
            });
        };

        //讀取生產批
        $scope.loadPlotList = function(){
            $MMWService.showQueryListPNIG({
                page_mode: 3,
                pageRecord:50,
                condition: [
                    {name:$filter('translate')('common.queryList.plot_no'), column_name:'LotNo', query_mode:'2', value:$scope.workItem.plot_no}
                ],
                header: [
                    {label:$filter('translate')('common.queryList.plot_no'), code:'page_no'}
                ],
                setCondition:function(conditions){
                    conditions.push({column_name:'status', query_mode:'0', value:'0', merge_condition_model:'and'});
                    conditions.push({column_name:'status', query_mode:'0', value:'1', merge_condition_model:'and'});
                    conditions.push({column_name:'status', query_mode:'0', value:'2', merge_condition_model:'and'});
                    return conditions;
                },
                selectCode:$scope.workItem.plot_no,
                confirm : function(selectItem){
                    $scope.workItem.plot_no = selectItem.page_no;
                }
            });
        };

        //選擇作業站
        $scope.loadOpNo = function(){
            $scope.$root.showQueryList({
                key:'op_no',
                header : [
                    {label: $filter('translate')('common.queryList.op_c'), code:'page_no'},
                    {label: $filter('translate')('common.queryList.op_n'), code:'page_name'}
                ],
                query: function(conditions, feedback){
                    $MMWService.pageNumberInfoGet({
                        page_mode : '4'
                    }, function(itemList){
                        if(itemList.length != 0){
                            feedback(itemList);
                        }else{
                            feedback();
                        }
                    });
                },
                selectCode:$scope.workItem.op_no||'',
                confirm: function(selectItem, dialog){
                    $scope.workItem.op_no = selectItem.page_no;
                    $scope.workItem.op_name = selectItem.page_name;
                    dialog.hide();
                    defaultQcItemGet_Imp();
                },
                cancel:function(dialog){
                    $scope.workItem.op_no = '';
                    $scope.workItem.op_name = '';
                    dialog.hide();
                }
            });
        };

        $scope.changeDate = function(){
            var date = $scope.workItem.plan_insp_date || moment().format('YYYY-MM-DD');
            $datePicker.open(date, function(dialog){
                $scope.workItem.plan_insp_date = dialog.datePacker.pickDate;
                dialog.hide();
            }, function(dialog){
                dialog.hide();
            }, {});
        };

        $scope.confirm = function(){
            var msg = beforeConfirm();
            if(msg == ''){
                qcFormCreateProcess(function(result){
                    $scope.$root.showAlert($filter('translate')('B0504.msg.success', {qc_form_no: result.qc_form_no}), function(){
                        $scope.workItem = {};
                        $scope.qc_item_detail = [];
                    });
                }, function(msg){
                    $scope.$root.showAlert($filter('translate')('B0504.msg.fail', {msg: msg}));
                });
            } else {
                $scope.$root.showAlert(msg);
            }
        }

        $scope.input_qty = function(event, item){
            var target = event.currentTarget;
            angular.element(target).addClass('is-focus');
            $calculater.open(function(num, dialog){
                angular.element(target).removeClass('is-focus');
                $scope.workItem.qty = num;
                dialog.hide();
            },
            function(dialog){
                angular.element(target).removeClass('is-focus');
                dialog.hide();
            },
            {
                defaultNum: $scope.workItem.qty,
                decimal: true
            });
        }

        //品管項目編輯
        $scope.open_qcItem_editor = function(){
            $QcItemEditService.init({
                workItem: $scope.workItem,
                selectList: $scope.qc_item_detail,
                confirm: function(dialog){
                    $scope.qc_item_detail = dialog.selectList;
                    dialog.hide();
                }
            });
        }

        function defaultQcItemGet_Imp(callback){
            var content = {
                item_type: $scope.workItem.item_type || '',
                item_no: $scope.workItem.item_no || '',
                op_no: $scope.workItem.op_no || '',
                inspect_type: $scope.workItem.inspect_type
            };

            //預設品管項目取得
            defaultQcItemGet(content, function(data){
                $scope.qc_item_detail = [];
                if(data.default_qc_item_get && data.default_qc_item_get.length > 0){
                    data.default_qc_item_get.forEach(function(item){
                        $scope.qc_item_detail.push({
                            qc_type : item.qc_item_type,
                            qc_item_no : item.qc_item_no,
                            qc_item_name : item.qc_item_name
                        });
                    });
                }
                if(callback)
                    callback();
            });
        }

        function beforeConfirm(){
            var msg = '';
            return msg;
        }

        //品號資訊取得
        function productInfoGet(content, callback){
            // var data = {};
            // data.product_info_get = [{
            //         item_no: '000',
            //         item_name: '測試用品號000',
            //         item_spec: '30cm',
            //         qc_category: '品管a類',
            //         article_no: '999999'
            //     },{
            //         item_no: '001',
            //         item_name: '測試用品號001',
            //         item_spec: '31cm',
            //         qc_category: '品管b類',
            //         article_no: '666666'
            //     }];
            
            console.log("品號資訊取得(product_info_get) send:", content);
            // if(callback)
            //     callback(data);

            $MMWService.sendToSMESServer({
                uri: 'tcSPC.clsSpc.product_info_get',
                content : content,
                success : function(data){
                    console.log("品號資訊取得(product_info_get) return:", data);
                    if(callback)
                        callback(data);
                }
            });
        }

        //檢驗單開立作業
        function qcFormCreateProcess(callback, error){
            var content = {
                // qc_form_no: $scope.workItem.qc_form_no,
                item_type: $scope.workItem.item_type,
                item_no: $scope.workItem.item_no,
                inspect_type: $scope.workItem.inspect_type,
                plot_no: $scope.workItem.plot_no,
                hotlot: ($scope.workItem.is_urgent == 'Y') ? '1' : '0',
                qty: $scope.workItem.qty || 0,
                op_no: $scope.workItem.op_no,
                plan_insp_date: $scope.workItem.plan_insp_date,
                memo: $scope.workItem.memo || '',
                qc_category: $scope.workItem.qc_category,
                article_no: $scope.workItem.article_no,
                spec: $scope.workItem.item_spec,
                unit: $scope.workItem.unit,
                qc_item_detail: $scope.qc_item_detail
            }
            console.log("檢驗單開立作業(qc_form_create_process) send:", content);
            // var data = {qc_form_no: 'TEST-123456789'};
            // if(callback)
            //     callback(data);

            $MMWService.sendToSMESServer({
                uri: 'tcSPC.clsSpc.qc_form_create_process',
                content : content,
                success : function(data){
                    console.log("檢驗單開立作業(qc_form_create_process) return:", data);
                    if(callback)
                        callback(data);
                },
                error: function(data){
                    if(error)
                        error(data.Exception);
                }
            });
        }

        //預設品管項目取得
        function defaultQcItemGet(content, callback){
            // var data = {};
            // data.default_qc_item_get = [
            //     {
            //         qc_item_type: '0',
            //         qc_item_no: '001',
            //         qc_item_name: '計量項目001'
            //     },{
            //         qc_item_type: '1',
            //         qc_item_no: '002',
            //         qc_item_name: '計數項目002'
            //     }
            // ];
            
            console.log("預設品管項目取得(default_qc_item_get) send:", content);
            // if(callback)
            //     callback(data);
            
            $MMWService.sendToSMESServer({
                uri: 'tcSPC.clsSpc.default_qc_item_get',
                content : content,
                success : function(data){
                    console.log("預設品管項目取得(default_qc_item_get) return:", data);
                    if(callback)
                        callback(data);
                }
            });
        }
    }
];});
